package de.gameduell.framework.locale {
	import de.gameduell.framework.debug.Debug;
	
	import flash.events.Event;	

	/**
	 * @author cbra
	 */
	public class DynamicContent {

		public static const DEFAULT:String	= "default";
		public static const DELIMITER:String = " ";
		
		private var localeData:LocaleData;
		private var locale:Locale;
		private var id:String;
		private var htmlEnabled:Boolean;
		private var localizable:Localizable;
		private var autoSize:String;
		private var node:XML;
		private var nodes:Array;
		private var updateVariables:Boolean;

		public function DynamicContent(aLocalizable:Localizable,aNodes:Array,aUpdateVariables:Boolean = true) {		
			locale	    = Locale.getInstance();
			localeData  = LocaleData.getInstance();
			nodes 	    = aNodes;		
			node 		= XML(nodes[0]);			
			id		    = String(node.name());
			htmlEnabled = node.attribute(Locale.HTML) == "true";
			localizable = aLocalizable;
			autoSize	= locale.getAutoSize(node);
			updateVariables = aUpdateVariables;
			initCondition();
			onVariableChanged();
		}
		
		private function initCondition():void{
			var variable:String = LocaleTools.splitCondition(node.attribute(Locale.CONDITION).toString())[0];
			if(variable != null){
				if(updateVariables){
					localeData.addEventListener(variable, onVariableChanged);
				}
			}
		}
		
		private function onVariableChanged(event:Event = null):void {
			checkCondition();
			checkVariables();
		}
		
		private function checkCondition():void {
			try{
				var matchingNode:XML = getNodeWithCondition();
				if(matchingNode != null){
					node = matchingNode;
					var currText:String = matchingNode.text();				
					setText(currText);	
					checkVariables();		
				}
			}catch(e:Error){
				Debug.trace("DynamicContent::conditionChanged id: "+id+" "+e, Debug.ERROR);
			}
		}
		
		private function textChanged(event:Event):void {
			checkVariables();
		}
		
		private function checkVariables():void {
			try {
				if(node.text().length()>0){
					var words:Array = String(node.text()).split(DELIMITER);				
					for(var i:* in words) {
						if(LocaleTools.containsVar(words[i])) {
							var key:String = LocaleTools.extractVarName(words[i]);
							if(updateVariables){
								localeData.addEventListener(key, textChanged);
							}
							words[i] = LocaleTools.replaceVar(words[i],localeData.getValue(key));
						}
					}	
					setText(words.join(DELIMITER));
				}
			}catch(e:Error) {
				Debug.trace("DynamicContent::checkVariables id: " + id + " " + e, Debug.ERROR);
			}
		}

		private function setText(aText:String):void{		
			if(!htmlEnabled){
				localizable.setText(aText, htmlEnabled, autoSize);
			}else{
				localizable.setText(LocaleTools.convertBrackets(aText), htmlEnabled, autoSize);
			}		
		}

		private function getNodeWithCondition():XML {
			try {
				if(locale.containsCondition(node)){
					var defaultNode:XML = null;
					for(var i:* in nodes) {
						var currNode:XML 		 = XML(nodes[i]);	
						var conditionText:String = LocaleTools.convertBrackets(currNode.attribute(Locale.CONDITION));
						var elements:Array 		 = LocaleTools.splitCondition(conditionText);
						var currVariable:String  = elements[0];
						var localValue:String 	 = localeData.getValue(currVariable);
						var comparison:String 	 = elements[1];
						var nodeValue:String 	 = elements[2];
						defaultNode = elements[2] == DEFAULT ? currNode : defaultNode;
	
						if(LocaleTools.compareCondition(nodeValue,comparison,localValue)){
							return currNode;
						}
					}
					return defaultNode;
				}
			}catch(e:Error) {
				Debug.trace("DynamicContent::getNodeWithCondition id:" + id + " condition not found");
			}
			return null;
		}

	}
}
